package frsf.cidisi.faia.designer;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class PanelDrawGUI extends JPanel{

    private int x1;
    private int y1;
    private int x2 = 0;
    private int y2 = 0;
    private int width; 
    private int high;
    private int form = 1; // "Line", "Rectangle," "Ellipse" 
    private int material =1 ; //"Wall", "glass", "linen", "Wood", "plastic" 
    private List objectList;
    private List materialList;
    private EnvironmentDesignerGUI p;
    //DetalleSensor sensor;;
    
    public PanelDrawGUI(EnvironmentDesignerGUI prin){
        p = prin;
        objectList = new ArrayList();
        materialList = new ArrayList();
        //sensor = new DetalleSensor(300,30,0.785398163,0.07,300, objectList);
        //sensor = new DetalleSensor(300,300,Math.PI+2,0.05,300, objectList);
        setDoubleBuffered(true);

        MouseListener ml = new MouseListener(){  
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if((form!=1)&&((x2<x1)||(y2<y1))){
                }
                else{
                    x2=e.getX();
                    y2=e.getY();
                    cargarFigura2D();
                    //ancho=e.getX();
                    //alto=e.getY();
                    p.repaint();
                    repaint();  
                }            
            }
   
            @Override
            public void mousePressed(MouseEvent e) {
                x1=e.getX();
                y1=e.getY();
            }
   
            @Override
            public void mouseExited(MouseEvent arg0) {;}
   
            @Override
            public void mouseEntered(MouseEvent arg0) {;}
   
            @Override
            public void mouseClicked(MouseEvent e) {;}
        };
  
        MouseMotionListener mml=new MouseMotionListener() {
   
            @Override
            public void mouseMoved(MouseEvent e) {;}

            @Override
            public void mouseDragged(MouseEvent e) {
                x2=e.getX();
                y2=e.getY();
                p.repaint();
                repaint();
            }
        };
        addMouseListener(ml);
        addMouseMotionListener(mml);
    }
 
    @Override
    public void paintComponent(Graphics g){   
        //super.paintComponents(g);
        Graphics2D g2d=(Graphics2D)g;
        cargarMapa(g);
  
        if(material == 1) g2d.setColor(Color.red);
        if(material == 2) g2d.setColor(Color.blue);  
        if(material == 3) g2d.setColor(Color.black);
        if(material == 4) g2d.setColor(Color.green);  
        if(material == 5) g2d.setColor(Color.cyan);  

        if(form == 1) g2d.drawLine(x1, y1, x2, y2);
        if(form == 2) g2d.drawRect(x1, y1, (x2-x1), (y2-y1));
        if(form == 3) g2d.drawOval(x1, y1, (x2-x1), (y2-y1));

        redibujar(g);
    }
 
    private void redibujar(Graphics g){       
        //super.paintComponents(g);
        Graphics2D g2d=(Graphics2D)g;
 
        for(int i = 0; i < objectList.size(); i++){      
            if(materialList.get(i) == 1) g2d.setColor(Color.red);
            if(materialList.get(i) == 2) g2d.setColor(Color.blue);  
            if(materialList.get(i) == 3) g2d.setColor(Color.black);
            if(materialList.get(i) == 4) g2d.setColor(Color.green);
            if(materialList.get(i) == 5) g2d.setColor(Color.cyan);  

            Class clase = objectList.get(i).getClass();
            if(clase.isInstance(new java.awt.geom.Line2D.Double())){
                Line2D.Double l = (Line2D.Double) objectList.get(i);     
                g2d.drawLine((int)l.getX1(), (int)l.getY1(), (int)l.getX2(), (int)l.getY2()); 
            }
            if(clase.isInstance(new java.awt.geom.Ellipse2D.Double())){
                Ellipse2D.Double l = (Ellipse2D.Double) objectList.get(i);     
                g2d.drawOval((int)l.x, (int)l.y, (int)(l.width), (int)(l.height)); 
            }
            /*
            g2d.setColor(Color.black);
            g2d.drawLine((int)sensor.getX1(), (int)sensor.getY1(), (int)sensor.getX2(), (int)sensor.getY2()); 
            g2d.setColor(Color.red);
            g2d.drawOval((int)sensor.puntoSensorX(), (int)sensor.puntoSensorY(), 4, 4);

            p.actualizarComponentes();
            */
            repaint();
        }
    }
 
    private void cargarMapa(Graphics g){
    
        Graphics2D g2d=(Graphics2D)g;
        Font sanSerifFont = new Font("SanSerif", Font.BOLD, 10);
        g2d.setFont(sanSerifFont);
        g.setColor(Color.gray);
        g.fillRect(0, 0, 640, 480);
        
        Stroke oldStroke = g2d.getStroke();
        float dash[] = {2.0f};
        g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dash, 1.0f));
  
        for(int i = 0; i<=680; i = i + 30){
            g.setColor(Color.lightGray);
            g2d.drawLine(0, i, 700, i);
            g2d.drawLine(i, 0, i, 500); 
            g2d.drawString(Integer.toString(i), 5, i);
            g2d.drawString(Integer.toString(i), i+1, 10);  
        }
        g2d.setStroke(oldStroke);
        /*
        dibujarSensor(g);
        dibujarRebote(g);
        dibujarSegundoRebote(g);
        */  
  }
    
/*
   private void dibujarCono(Graphics g){
    
     Graphics2D g2d=(Graphics2D)g;
     List lista = sensor.particionarLinea();
     for(int i = 0; i < lista.size(); i=i+10)
     {
          Rectangle2D.Double d = java.awt.geom.Rectangle2D.Double.class.cast(lista.get(i));
          g2d.drawRect((int)(d.getMaxX()-d.width), (int)(d.getMaxY()-d.height), (int)d.width, (int)d.height); 
     }
   }
   
    private void dibujarSensor(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        g2d.setColor(Color.black);
        g2d.drawLine((int)sensor.getX1(), (int)sensor.getY1(), (int)sensor.getX2(), (int)sensor.getY2()); 
        g2d.setColor(Color.red);
        g2d.drawOval((int)sensor.puntoSensorX(), (int)sensor.puntoSensorY(), 2, 2);
        g2d.drawOval((int)sensor.puntoSensorX(), (int)sensor.puntoSensorY(), 3, 3);
        g2d.drawOval((int)sensor.puntoSensorX(), (int)sensor.puntoSensorY(), 6, 6);

        g2d.setColor(Color.orange);
        Font san = new Font("SanSerif", Font.BOLD, 14);
        g2d.setFont(san);
        g2d.drawString("Sensor", (int)sensor.puntoSensorX()+3, (int)sensor.puntoSensorY()+5);  

        // dibujarCono(g2d);     
    }
  
    private void dibujarRebote(Graphics g){
        if(sensor.rebote()){
            Graphics2D g2d=(Graphics2D)g;
            g2d.setColor(Color.ORANGE);
            
            //System.out.printf(" "+ sensor.getRebote().getX1() +"  "+sensor.getRebote().getY1()+"  "+sensor.getRebote().getX2()+"   "+sensor.getRebote().getAnguloApertura()+"   "+sensor.getRebote().getDistancia()+"\n");

            g2d.drawLine((int)(sensor.getRebote()).getX1(), (int)(sensor.getRebote()).sensor.getY1(), (int)(sensor.getRebote()).sensor.getX2(), (int)(sensor.getRebote()).sensor.getY2()); 
            g2d.setColor(Color.red);
            g2d.drawOval((int)sensor.getRebote().puntoSensorX(), (int)sensor.getRebote().puntoSensorY(), 2, 2);
            g2d.drawOval((int)sensor.getRebote().puntoSensorX(), (int)sensor.getRebote().puntoSensorY(), 3, 3);
            g2d.drawOval((int)sensor.getRebote().puntoSensorX(), (int)sensor.getRebote().puntoSensorY(), 6, 6);
            g2d.setColor(Color.orange);
            Font san = new Font("SanSerif", Font.BOLD, 14);
            g2d.setFont(san);
            g2d.drawString("Rebote..", (int)sensor.getRebote().puntoSensorX()+3, (int)sensor.getRebote().puntoSensorY()+5);   
        } 
        //dibujarCono(g2d);
    }
    
    private void dibujarSegundoRebote(Graphics g){
    
        DetalleSensor s = sensor.rebote;
        if(s==null){return;}
        if(s.rebote()){
            Graphics2D g2d=(Graphics2D)g;
            g2d.setColor(Color.ORANGE);
            
            //System.out.printf(" "+ sensor.getRebote().getX1() +"  "+sensor.getRebote().getY1()+"  "+sensor.getRebote().getX2()+"   "+sensor.getRebote().getAnguloApertura()+"   "+sensor.getRebote().getDistancia()+"\n");

            g2d.drawLine((int)(s.getRebote()).getX1(), (int)(s.getRebote()).sensor.getY1(), (int)(s.getRebote()).sensor.getX2(), (int)(s.getRebote()).sensor.getY2()); 
            g2d.setColor(Color.red);
            g2d.drawOval((int)s.getRebote().puntoSensorX(), (int)s.getRebote().puntoSensorY(), 2, 2);
            g2d.drawOval((int)s.getRebote().puntoSensorX(), (int)s.getRebote().puntoSensorY(), 3, 3);
            g2d.drawOval((int)s.getRebote().puntoSensorX(), (int)s.getRebote().puntoSensorY(), 6, 6);
            g2d.setColor(Color.orange);
            Font san = new Font("SanSerif", Font.BOLD, 14);
            g2d.setFont(san);
            g2d.drawString("Segundo Rebote", (int)s.getRebote().puntoSensorX()+3, (int)s.getRebote().puntoSensorY()+5); 
         }
    }
*/
    private void cargarFigura2D(){
       
        materialList.add(new Integer(this.getMaterial()));
   
        if(form == 1 ){
            Line2D.Double l = new Line2D.Double(x1,y1,x2,y2);
            objectList.add(l);
        }
        if(form == 2){
            Rectangle2D.Double l = new Rectangle2D.Double();
            l.setFrameFromDiagonal(x1, y1, x2, y2);
            List l1 = new ArrayList();
            l1 = this.RectanguloALinea(l);     
            materialList.add(new Integer(this.getMaterial()));
            materialList.add(new Integer(this.getMaterial()));
            materialList.add(new Integer(this.getMaterial()));
            for(int i=0;i<4;i++){
                objectList.add(l1.get(i));
            }
        }
        if(form == 3){
            Ellipse2D.Double l = new Ellipse2D.Double();
            l.setFrameFromCenter((x2+x1)/2, (y2+y1)/2, x2, y2);
            //x1, y1, (x2-x1), (y2-y1)
            objectList.add(l);
        }
    }
   
    public List RectanguloALinea(Rectangle2D.Double rect){
        List lista = new ArrayList();
        Line2D.Double l1 = new Line2D.Double(rect.getMaxX(),rect.getMaxY(), rect.getMaxX(), rect.getMinY());
        Line2D.Double l2 = new Line2D.Double(rect.getMaxX(),rect.getMaxY(), rect.getMinX(), rect.getMaxY());
        Line2D.Double l3 = new Line2D.Double(rect.getMinX(),rect.getMinY(), rect.getMaxX(), rect.getMinY());
        Line2D.Double l4 = new Line2D.Double(rect.getMinX(),rect.getMinY(), rect.getMinX(), rect.getMaxY());
       
        lista.add(l1);
        lista.add(l2);
        lista.add(l3);
        lista.add(l4);
        
        return lista;
    }
 
    private void MostrarCoordenadas(int a, int b){
        System.out.println(" " + a + " " + b + "\n");
    }

    public List getMaterialList() {
        return materialList;
    }

    public void setMaterialList(List materialList) {
        this.materialList = materialList;
    }

    public List getObjectList() {
        return objectList;
    }

    public void setObjectList(List objectList) {
        this.objectList = objectList;
    }

    public EnvironmentDesignerGUI getP() {
        return p;
    }

    public void setP(EnvironmentDesignerGUI p) {
        this.p = p;
    }
       
       
    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public int getX2() {
        return x2;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public int getY2() {
        return y2;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHigh() {
        return this.high;
    }

    public void setHigh(int high) {
        this.high = high;
    }

    public int getForm() {
        return this.form;
    }

    public void setForm(int form) {
        this.form = form;
    }
 
    public void setMaterial(int mate){
        this.material = mate;
        repaint();
    }
    
    public int getMaterial(){
        return this.material;
    } 
 }
        
        

